package Interceptor;

import com.google.common.collect.ImmutableMap;
import common.pojo.CommonResult;
import common.utils.JsonUtils;
import httpclient.HttpClientUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import pojo.UserShopInfo;
import service.cfg.ShopCfg;
import service.cfg.WxUserCfg;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by asus on 2018/11/20.
 */
@Service
public class MyWebSocketInterceptor implements HandshakeInterceptor  {


    @Autowired
    ShopCfg shopCfg;
    @Override
    public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
        if (serverHttpRequest instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) serverHttpRequest;
            HttpServletRequest httpRequest = servletRequest.getServletRequest();
            //获取参数//验证用户身份
           Map param =  this.parseRequestParamMap(httpRequest);
           if(param.containsKey("type") && param.get("type")!=null){
               //企业号过来的
               if("business".equals(param.get("type").toString())){
                   if(StringUtils.isNotBlank(param.get("wxId").toString())){
                       if (param.containsKey("shopId") && param.get("shopId")!=null){}else {
                           CommonResult commonResult = HttpClientUtils.get(shopCfg.getShopInfo(), ImmutableMap.of("wxId",param.get("wxId").toString()));
                           UserShopInfo shopInfo = JsonUtils.objectToPojo(commonResult.getData(), UserShopInfo.class);
                           if(shopInfo==null){
                               return false;
                           }
                           param.put("shopId",shopInfo.getId());
                       }
                   }
               }
           }
           map.putAll(param);
        }
        System.out.println("连接到我了");
        return true;

    }

    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {

    }


    final protected Map<String,Object> parseRequestParamMap(HttpServletRequest request) {
        Enumeration<String> argEnu = request.getParameterNames();
        Map<String,Object> args=new HashMap();
        while (argEnu.hasMoreElements()) {
            String argName = argEnu.nextElement();
            String[] value = request.getParameterValues(argName);
            if (value == null) {
                continue;
            }
            for (String v : value) {
                args.put(argName,v);
            }
            if(value.length>1){
                args.put(argName,value);
            }
        }
        return args;
    }
}
